﻿using Microsoft.Extensions.DependencyInjection;
using MyWeb.Extensions.AutoMapper;
using MyWeb.Extensions.Nacos;
using MyWeb.Extensions.ServiceExtensions;
using MyWeb.Extensions.ServiceExtensions.Authorization;
using MyWeb.Extensions.ServiceExtensions.Filter;
using MyWeb.Extensions.ServiceExtensions.RedisCache;
using MyWeb.Extensions.ServiceExtensions.SerializationFormat;
using MyWeb.Extensions.ServiceExtensions.Sqlsugar;
using MyWeb.Extensions.ServiceExtensions.Swagger;
using MyWeb.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyWeb.Extensions
{
    /// <summary>
    /// 项目启动服务
    /// </summary>
    public static class ProgramSetup
    {
        /// <summary>
        /// Extensions 启动服务
        /// </summary>
        /// <param name="services"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public static void AddProgramSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            services.AddHttpContextAccessor();
            //json格式序列化规则
            services.AddSerializationFormatSetup();
            //注入Swagger
            services.AddSwaggerSetup();
            //SqlSugar 启动服务
            services.AddSqlsugarSetup();
            //Automapper 启动服务
            services.AddAutoMapperSetup();
            //Redis 消息队列 启动服务
            services.AddRedisInitMqSetup();
            //Redis缓存 启动服务
            services.AddRedisCacheSetup();
            //Memory缓存 启动服务
            services.AddMemoryCacheSetup();
            //系统 授权服务 配置
            services.AddAuthorizationSetup();
            //请求拦截器 启动服务
            services.AddFilterSetup();
            // 配置跨域处理，允许所有来源
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolocy", p =>
                {
                    p.AllowAnyOrigin()//允许所有Origin策略
                    //.WithOrigins("*")
                    .AllowAnyMethod()//允许所有请求方法：get,post.put,delete
                    .AllowAnyHeader();//允许所有请求头：application/json
                    //.AllowCredentials();
                });
            });
            // 配置任务调度
            //services.AddQuartzSetup();
            // 配置Nacos服务
            services.AddNacosSetup();

            services.AddScoped<IOperatorProvider,OperatorProvider>();
        }
    }
}
